package com.example.controller;

import com.example.common.Result;
import com.example.entity.Employee;
import com.example.service.EmployeeService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    /**
     * 新增数据
     * @param employee
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Employee employee){
        employeeService.add(employee);
        return Result.success();
    }
    /**
     * 更新数据
     */
    @PutMapping("/updateById")
    public Result updateById(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return Result.success();
    }

    /**
     * 删除单个数据
     */
    @DeleteMapping("/deleteById/{id}")
    public Result delete(@PathVariable Integer id){
        employeeService.deleteById(id);
        return Result.success();
    }

    /**
     * 删除多个数据
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        employeeService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 查询单个数据
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Employee employee =  employeeService.selectById(id);
        return Result.success(employee);
    }

    /**
     * 查询所有的数据
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(Employee employee) {
        List<Employee> list =  employeeService.selectAll(employee);
        return Result.success(list);
    }

//    /**
//     * 查询单个数据
//     * @param id
//     * @return
//     */
//    @GetMapping("/selectOne")
//    public Result selectOne(@RequestParam Integer id){
//        Employee employee = employeeService.selectById(id);
//        return Result.success(employee);
//    }



//    @GetMapping("/selectList")
//    public Result selectList(Employee employee){
//        List<Employee> list = employeeService.selectList(employee);
//        return Result.success(list);
//    }
    /**
     * 分页查询数据
     * pageNum: 当前页码
     * pageSize: 每页显示几个数据
     */
    @GetMapping("/selectPage")
    public Result selectPage(Employee employee,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize){
        PageInfo<Employee> PageInfo = employeeService.selectPage(employee,pageNum, pageSize);
        return Result.success(PageInfo);
    }


}
